package qldc.pm.tiensu.searchcontainer;

import javax.portlet.PortletRequest;
import javax.portlet.PortletURL;

import qldc.pm.tiensu.searchcontainer.util.bienbanbangiao.BienBanBanGiaoComparatorUtil;

import com.liferay.portal.kernel.dao.search.SearchContainer;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.OrderByComparator;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portlet.PortalPreferences;
import com.liferay.portlet.PortletPreferencesFactoryUtil;

public class BienBanBanGiaoHoSoSearch extends SearchContainer<BienBanBanGiaoHoSoSearch> {
	public static final String EMPTY_RESULTS_MESSAGE = "no-web-content-were-found";

	public BienBanBanGiaoHoSoSearch(PortletRequest portletRequest, PortletURL iteratorURL) {
		super(portletRequest, new BienBanBanGiaoHoSoDisplayTerms(portletRequest),
				new BienBanBanGiaoHoSoSearchTerms(portletRequest), DEFAULT_CUR_PARAM,
				DEFAULT_DELTA, iteratorURL, null, EMPTY_RESULTS_MESSAGE);

		BienBanBanGiaoHoSoDisplayTerms displayTerms = (BienBanBanGiaoHoSoDisplayTerms) getDisplayTerms();

		iteratorURL.setParameter(BienBanBanGiaoHoSoSearchTerms.NGUOI_DUOC_BAN_GIAO , displayTerms.getNguoiDuocBanGiao());
		iteratorURL.setParameter(BienBanBanGiaoHoSoSearchTerms.CHUC_VU_NGUOI_DUOC_BAN_GIAO , displayTerms.getChucVuNguoiDuocBanGiao());

		try {
			PortalPreferences preferences = PortletPreferencesFactoryUtil.getPortalPreferences(portletRequest);

			String orderByCol = ParamUtil.getString(portletRequest, "orderByCol");
			String orderByType = ParamUtil.getString(portletRequest,"orderByType");
			String portletName = "tiensu";

			if(Validator.isNotNull(orderByCol) && Validator.isNotNull(orderByType)){
				preferences.setValue(portletName, "order-by-col", orderByCol);
				preferences.setValue(portletName, "order-by-type", orderByType);

			} else{
				orderByCol = preferences.getValue(portletName, "order-by-col", "");
				orderByType = preferences.getValue(portletName, "order-by-type", "asc");
			}

			OrderByComparator orderByComparator = BienBanBanGiaoComparatorUtil.getBienBanBanGiaoOrderByComparator(orderByCol, orderByType);

			setOrderByCol(orderByCol);
			setOrderByType(orderByType);
			setOrderByComparator(orderByComparator);
		} catch (SystemException e) {
			System.out.println("E "+e.getMessage());
 		}
	}
}
